<template>
  <a-row id="globalHeader" align="center">
    <a-col flex="auto">
      <div class="menu">
        <a-menu
          mode="horizontal"
          :default-selected-keys="selectedKeys"
          @menu-item-click="domenuClick"
        >
          <a-menu-item
            key="0"
            :style="{ padding: 0, marginRight: '38px' }"
            disabled
          >
            <div class="title-bar">
              <img class="logo" src="../assets/oj-logo.jpg" />
              <div class="title">智谱-OJ</div>
            </div>
          </a-menu-item>
          <a-menu-item v-for="item in visibleRoutes" :key="item.path">
            {{ item.name }}
          </a-menu-item>
        </a-menu>
      </div>
    </a-col>
    <a-col flex="100px">
      <div>{{ store.state.user?.loginUser?.userName ?? "未登录" }}</div>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "@/router/routers";
import { useRoute, useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import cheakAccess from "@/access/cheakAccess";

// 路由组件
const router = useRouter();
// 获取路由信息
const route = useRoute();

const store = useStore();

// 默认主页
const selectedKeys = ref(["/"]);
const domenuClick = (key: string) => {
  router.push({
    path: key,
  });
};
// 在路由跳转后
router.afterEach((to, from) => {
  selectedKeys.value = [to.path];
});
// 展示在菜单的路由数组
const visibleRoutes = computed(() => {
  // 变量要写在方法里面
  const loginUser = store.state.user.loginUser;
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    // 根据权限过滤菜单
    if (!cheakAccess(loginUser, item?.meta?.access as string)) {
      return false;
    }
    return true;
  });
});
//
// setTimeout(() => {
//   store.dispatch("user/getLoginUser", {
//     userName: "ortonzhu",
//     userRole: ACCESS_ENUM.ADMIN,
//   });
// }, 3000);
</script>

<style scoped></style>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.logo {
  height: 64px;
}

.title {
  color: #444;
  margin-left: 10px;
}

.menu {
  width: 100%;
  background-color: var(--color-neutral-2);
  text-align: left;
}
</style>
